package com.mixsmart.commonweb.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mixsmart.commonweb.IWebConstant;
import com.mixsmart.commonweb.bean.BaseTree;
import com.mixsmart.commonweb.bean.entity.TCMenu;
import com.mixsmart.commonweb.bean.entity.TCRoleMenu;
import com.mixsmart.commonweb.dao.PKStringDao;
import com.mixsmart.commonweb.helper.TreeHelper;
import com.mixsmart.commonweb.service.IMenuService;
import com.mixsmart.commonweb.service.ITreeService;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.xml.sqlmap.SqlMapUtils;
import com.mixsmart.core.xml.sqlmap.bean.SqlUnit;
import com.mixsmart.utils.CollectionUtils;
import com.mixsmart.utils.StringUtils;

/**
 * 菜单服务实现类
 * 
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6 
 * <p>2016年2月1日</p>
 */
@Service
public class MenuService extends TreeService<TCMenu> implements IMenuService, ITreeService<TCMenu> {

	@Autowired
	private ServiceContext servContext;

	@Override
	public List<TCMenu> getMenuItemsByRole(List<String> roleIds) {
		List<TCMenu> menus = null;
		HashMap<String, Object> params = null;
		if (CollectionUtils.isNotEmpty(roleIds)) {
			params = new HashMap<String, Object>();
			params.put("roleIds", roleIds.toArray());
		}
		SqlUnit sqlUnit = SqlMapUtils.getSql("menu_assoc_res_list_by_role");
		if (null != sqlUnit) {
			logger.info("正在执行资源名称为["+sqlUnit.getName()+"]对应的SQL语句---");
			if (sqlUnit.isFilter()) {
				menus = getQueryServ().querySqlFilter(sqlUnit.getSql(),params, TCMenu.class);
			} else {
				menus = getQueryServ().querySql(sqlUnit.getSql(), params,TCMenu.class);
			}
		} else {
			logger.error("没有获取到资源名称[menu_assoc_res_list_by_role]对应的SQL语句....");
		}
		return menus;
	}

	@Override
	public MixResponse<BaseTree<TCMenu>> getMenuTreeByRole(List<String> roleIds) {
		MixResponse<BaseTree<TCMenu>> mixResp = new MixResponse<BaseTree<TCMenu>>();
		List<BaseTree<TCMenu>> menuTrees = null;
		List<TCMenu> menus = this.getMenuItemsByRole(roleIds);
		if (CollectionUtils.isNotEmpty(menus)) {
			TreeHelper<TCMenu> treeHelper = new TreeHelper<TCMenu>();
			try {
				menuTrees = treeHelper.outPutTree(menus);
				if (CollectionUtils.isNotEmpty(menuTrees)) {
					mixResp.setResult(IWebConstant.OP_SUCCESS);
					mixResp.setDatas(menuTrees);
					mixResp.setMsg(IWebConstant.OP_SUCCESS_MSG);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return mixResp;
	}

	@Override
	public MixResponse<BaseTree<TCMenu>> getMenuTreeByRole() {
		MixResponse<BaseTree<TCMenu>> mixResp = new MixResponse<BaseTree<TCMenu>>();
		List<BaseTree<TCMenu>> menuTrees = null;
		try {
			List<TCMenu> menus = this.getMenuItemsByRole(null);
			if (CollectionUtils.isNotEmpty(menus)) {
				TreeHelper<TCMenu> treeHelper = new TreeHelper<TCMenu>();
				menuTrees = treeHelper.outPutTree(menus);
				if (CollectionUtils.isNotEmpty(menuTrees)) {
					mixResp.setResult(IWebConstant.OP_SUCCESS);
					mixResp.setDatas(menuTrees);
					mixResp.setMsg(IWebConstant.OP_SUCCESS_MSG);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mixResp;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean save(TCMenu t) {
		boolean is = false;
		if (super.saveTree(t)) {
			// 把菜单关联添加到超级管理员角色中
			String superRoleId = servContext.getRoleServ().getSuperRoleId();
			if (StringUtils.isNotEmpty(superRoleId)) {
				is = super.saveObj(new TCRoleMenu(superRoleId, t.getId()));
			}
			is = true;
		}
		return is;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean update(TCMenu t){
		return super.updateTree(t);
	}

	@Override
	public PKStringDao<TCMenu> getDao() {
		return (PKStringDao<TCMenu>) dao;
	}
}
